package com.junzhiit.nengji.shop.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.serializer.Format;
import com.junzhiit.nengji.bo.ModulesAdvertiseBO;
import com.junzhiit.nengji.bo.ShopBO;
import com.junzhiit.nengji.bo.ShopDetailsBO;
import com.junzhiit.nengji.bo.ShopWithinDistanceBO;
import com.junzhiit.nengji.dao.FavoriteShopDAO;
import com.junzhiit.nengji.dao.ModulesAdvertiseDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.UserEvaluateDAO;
import com.junzhiit.nengji.model.FavoriteShop;
import com.junzhiit.nengji.model.enums.IsChainStatus;
import com.junzhiit.nengji.shop.service.ShopService;
import com.junzhiit.nengji.shop.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.shop.web.util.AmapUtil;
import com.junzhiit.nengji.shop.web.vo.ResultVO;
import com.junzhiit.nengji.shop.web.vo.ShopVO;

@Service
public class ShopServiceImpl extends BaseServiceImpl implements ShopService {
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired 
	private FavoriteShopDAO favoriteShopDAO;
	
	@Autowired
	private UserEvaluateDAO userEvaluateDAO;
	
	@Autowired
	private ModulesAdvertiseDAO modulesAdvertiseDAO;

	@Override
	public ShopVO getById(String userId,String shopId) {
		
		ShopBO shopBO= shopDAO.findShopById(shopId);
		if (shopBO == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
//		int count= userDAO.checkMember(userId);
//		if (count < 0) {
//			throw new UserNotFoundException("用户不存在");
//		}
		
		ShopVO shopVO=new ShopVO();
		BeanUtils.copyProperties(shopBO, shopVO);
		
		Double score= userEvaluateDAO.getShopEvaluateScore(shopId);
		shopVO.setScore(score==null?"0":Format.decimalToString(score,Format.DECIMAL_N));
		
		String Images = shopBO.getBannerImages();
		List<String> bannerImages=new ArrayList<String>();
		if (StringUtils.isNotBlank(Images)) {
			bannerImages = Arrays.asList(Images.split(","));
		}
		shopVO.setBannerImages(bannerImages);
		
		FavoriteShop favoriteShop=favoriteShopDAO.getByCondition(userId, shopId);
		if(favoriteShop!=null){
			shopVO.setFavorite(true);
			shopVO.setFavoriteShopId(favoriteShop.getId());
		}else{
			shopVO.setFavorite(false);
		}

		return shopVO;
	}

	@Override
	public ResultVO findShops(String name, String userId) {
		ResultVO result = new ResultVO();
		List<ShopDetailsBO> shopList = shopDAO.findShops(name);
		result.setData(shopList);
		return result;
	}

	@Override
	public ResultVO withinDistanceShop(String locations,String coordsys, Double distance) {
		ResultVO result = new ResultVO();
		AmapUtil.Location location = AmapUtil.convert(locations, coordsys);
		List<ShopWithinDistanceBO> list = shopDAO.withinDistanceShop( location.getLat(),location.getLon(), distance);
		result.setData(list);
		return result;
	}

	@Override
	public ResultVO aroundShop(String locations, String coordsys, Integer isChain, String csId) {
		ResultVO result = new ResultVO();
		Map<String,Object> map = new HashMap<String,Object>();
		ShopBO shopBO = null;
		if(isChain == IsChainStatus.YES.toIntStatus()){
			String []src = locations.split(",");
			shopBO = shopDAO.aroundShop(Double.valueOf(src[0]),Double.valueOf(src[1]),csId);
		}else{
			shopBO = shopDAO.findShopById(csId);
		}
		// 查询栏目的所有模块的所有模块广告
		List<ModulesAdvertiseBO> modulesAdvertiseBOList = modulesAdvertiseDAO.findModulesAdvertise(null, shopBO.getShopId());
		map.put("shop", shopBO);
		map.put("modulesAdvertise", modulesAdvertiseBOList);
		result.setData(map);
		return result;
	}

	@Override
	public ResultVO chainShop(String locations, String csId, String areaCityId) {
		ResultVO result = new ResultVO();
		String []src = locations.split(",");
		List<ShopBO> list = shopDAO.chainShop(Double.valueOf(src[0]),Double.valueOf(src[1]), csId, areaCityId);
		result.setData(list);
		return result;
	}
	
}
